package 除法;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 除法的关键：保留除数，余数*10后，继续前面的操作。
 */
public class division {
    public static void main(String[] args) {
        division(11,13);
        System.out.println();

        List<Integer> number= divisionOptimization(11,13,10);
        System.out.print(number.get(0)+".");
        for (int j = 1; j < number.size(); j++) {
            System.out.print(number.get(j));
        }
        System.out.println();

    }
    public static void division(int x,int y){
        List<Integer> number = new ArrayList<>();
        int remainder = x%y; // 余数
        number.add(0,x/y);
        int numberOfDigits = 20;
        System.out.print(number.get(0)+".");
        for (int i = 1; i <= numberOfDigits; i++) {
            number.add(i,(remainder*10)/y);
            remainder = (remainder*10)%y;
            System.out.print(number.get(i));
        }
    }
    public static void division(int x,int y,int reservedDigits){
        List<Integer> number = new ArrayList<>();
        int remainder = x%y; // 余数
        number.add(0,x/y);
        System.out.print(number.get(0)+".");
        for (int i = 1; i <= reservedDigits; i++) {
            number.add(i,(remainder*10)/y);
            remainder = (remainder*10)%y;
            System.out.print(number.get(i));
        }
    }
    public static List<Integer> divisionOptimization(int x, int y, int reservedDigits){
        List<Integer> number = new ArrayList<>();
        int remainder = x%y; // 余数
        number.add(0,x/y);
        for (int i = 1; i <= reservedDigits; i++) {
            number.add(i,(remainder*10)/y);
            remainder = (remainder*10)%y;
        }
        return number;
    }
}
